<?php
	
	if(getRequestParameter("sumbit_question") !== false)
	{
		$errors = array();
		$messages = array();
		
		if(getRequestParameter("name") != false)
		{
			$params["name"] = getRequestParameter("name");
		}
		else
		{
			$errors["name"] = translate("comment_error_name");
		}
		
		if(getRequestParameter("email") != false)
		{
			$params["email"] = getRequestParameter("email");
			
			if(validateEmail($params["email"]) != true)
			{
				$errors["email"] = translate("comment_error_invalid_email");
			}
		}
		else
		{
			$errors["email"] = translate("comment_error_email");
		}
		
		if(getRequestParameter("question") != false)
		{
			$params["question"] = getRequestParameter("question");
		}
		else
		{
			$errors["comment"] = translate("question_error_message");
		}
		
		if(getRequestParameter("product_id") !== false && getRequestParameter("product_id") > 0)
		{
			
			if(count($errors) == 0)
			{
				global $connection;
				
				$params["ip"] = $_SERVER['REMOTE_ADDR'];
				$params["date"] = time();
				$params["language_id"] = $_SESSION["language"]["id"];
				$params["visible"] = 0;
				$params["product_id"] = getRequestParameter("product_id");
				
				$questionID = $connection->insert("products_questions", $params);
				
				$messages["success"] = translate("question_success");
				
				if(get_setting("questions_email_address") != "")
				{
					$site = verify_site(true);
					
					if(isset($site["name"]))
					{
						include("../helpers/Email.php");
						
						$content = translate("from")." : ".$params["name"].", ".translate("email"). " : ".$params["email"];
						$content .= "<br />".$params["question"];
						
						$email = new Email();
						$config['mailtype'] = 'html';
						
						$companyEmail = get_setting("questions_email_address");
						$companyName = get_setting("company_name");
						
						$email->initialize ( $config );
						$email->from($params["email"], $params["name"]);
						$email->reply_to(get_setting("questions_email_address"));
						$email->to(get_setting("contact_email")); 
						//$email->cc('we@yahoo.com'); 
						//$email->bcc('them@their-example.com'); 
						$email->subject(translate("order_email")." -".$site["name"]."- #".$companyName);
						$email->message($content);	
						$email->send();
					}
					
				}
			}
		}
		else
		{
			$errors["unknown_error"] = translate("unknown_error");
		}
		
		$errorCount = count($errors);
		$messages["error_count"] = $errorCount;
		$messages["errors"] = $errors;
		
		echo json_encode($messages);
		
		return false;
	}
	
	
	
	
	
	
	if(getRequestParameter("sumbit_comment") !== false)
	{
		$errors = array();
		$messages = array();
		
		if(getRequestParameter("name") != false)
		{
			$params["name"] = getRequestParameter("name");
		}
		else
		{
			$errors["name"] = translate("comment_error_name");
		}
		
		if(getRequestParameter("email") != false)
		{
			$params["email"] = getRequestParameter("email");
			
			if(validateEmail($params["email"]) != true)
			{
				$errors["email"] = translate("comment_error_invalid_email");
			}
			
		}
		else
		{
			$errors["email"] = translate("comment_error_email");
		}
		
		if(getRequestParameter("comment") != false)
		{
			$params["content"] = getRequestParameter("comment");
		}
		else
		{
			$errors["comment"] = translate("comment_error_message");
		}
		
		if(getRequestParameter("item_id") !== false && getRequestParameter("item_id") > 0)
		{
			
			if(getRequestParameter("comment_type") !== false)
			{
				if(getRequestParameter("comment_type") == "article")
				{
					$table = "articles_comments";
					$field = "article_id";
				}
				elseif(getRequestParameter("comment_type") == "product")
				{
					$table = "products_comments";
					$field = "product_id";
				}
			}
			
		}
		
		
		if(isset($table) && isset($field) && $table != "" && $field != "")
		{
			if(count($errors) == 0)
			{
				global $connection;
				
				$params["ip"] = $_SERVER['REMOTE_ADDR'];
				$params["date"] = time();
				$params["language_id"] = $_SESSION["language"]["id"];
				$params["visible"] = 0;
				
				$data["comment_id"] = $connection->insert("comments", $params);
				$data[$field] = getRequestParameter("item_id");
				
				$connection->insert($table, $data);
				
				$messages["success"] = translate("comment_success");
				
			}
		}
		else
		{
			$errors["unknown_error"] = "asd".translate("unknown_error");
		}
		
		
		$errorCount = count($errors);
		$messages["error_count"] = $errorCount;
		$messages["errors"] = $errors;
		
		echo json_encode($messages);
		
		return false;
	}
	
	if(getRequestParameter("contact") !== false)
	{
		require_once("../helpers/securimage/securimage.php");
		
		$errors = array();
		$params = array();
		$message = array();
		
		$securimage = new Securimage();
		
		if($securimage->check(getRequestParameter("captcha")) == false) 
		{
			$errors["captcha"] = translate("contact_error_invalid_captcha");
		}
		
		if(getRequestParameter("name") != false)
		{
			$params["name"] = getRequestParameter("name");
		}
		else
		{
			$errors["name"] = translate("contact_error_name");
		}
		
		
		if(getRequestParameter("email") != false)
		{
			$params["email"] = getRequestParameter("email");
			
			if(validateEmail($params["email"]) != true)
			{
				$errors["email"] = translate("contact_error_invalid_email");
			}
			
		}
		else
		{
			$errors["email"] = translate("contact_error_email");
		}
		
		if(getRequestParameter("message") != false)
		{
			$params["message"] = getRequestParameter("message");
		}
		else
		{
			$errors["message"] = translate("contact_error_message");
		}
	
		$errorCount = count($errors);
		
		if($errorCount > 0)
		{
			$errors["error_count"] = $errorCount;
			echo json_encode($errors);
			return false;
		}
		else
		{
			require_once("../helpers/Email.php");
			
			$emailContent = "	<p>Name: <strong>{$params["name"]}</strong></p>
								<p>Email: <strong>{$params["email"]}</strong></p>
								<p>Message:<br /> <strong><pre>{$params["message"]}<pre></strong></p>";
								
			$SMTPhost = get_setting("smtp_host");
			$SMTPuser = get_setting("smtp_user");
			$SMTPpass = get_setting("smtp_pass");
			$SMTPport = get_setting("smtp_port");
			
			if($SMTPhost != "" && $SMTPuser != "" && $SMTPpass != "" && $SMTPport != "")
			{
				$email = new Email();
				
				$config['mailtype'] = 'html';
				
				
				
				$config["smtp_host"] = "mail.zacuscalabs.com";
				$config["smtp_user"] = "alpar.lorincz@zacuscalabs.com";
				$config["smtp_pass"] = "2RarEwra";
				$config["smtp_port"] = "26";
				
				$companyEmail = get_setting("contact_email");
				$companyName = $site["name"];
				
				$email->initialize ( $config );
				$email->from($params["email"], $params["name"]);
				$email->to($companyEmail); 
				
				$email->subject('New contact request');
				$email->message($emailContent);	
				
	// TODO : decomment this			
				
				//$email->send();
			}
			
			$params["ip"] = $_SERVER['REMOTE_ADDR'];
			$params["date"] = date("Y-m-d H-i-s");
			$params["created"] = time();
			$params["language_id"] = $_SESSION["language"]["id"];
			
			
			
			global $connection;
			$connection->insert("contact_requests", $params);
			
			$message["error_count"] = 0;
			$message["message"] = translate("contact_success");
			echo json_encode($message);
		}
	}

	if(getRequestParameter("get_counties") !== false)
	{
		$countryID = getRequestParameter("country_id");
		generateCountyList($countryID);
	}
	
	if(getRequestParameter("get_cities") !== false)
	{
		//$countryID = getRequestParameter("country_id");
		$countyID = getRequestParameter("county_id");
		generateCityList($countyID);
	}
	
?>